//导入Vue包
import Vue from 'vue'
//导入VueRouter包
import VueRouter from 'vue-router'

import {getToken} from '@/utils/auth'

import api from '@/api/api'
//导入组件
// import Login from '@/views/Login'
// import Register from '@/views/Register'
// import Home from '@/views/Home'
// import workplace from '@/views/dashboard/workplace.vue'
// import analysis from '@/views/dashboard/analysis.vue'
// import studentList from '@/views/students/studentList.vue'
//将VueRouter作为插件设置到Vue中
Vue.use(VueRouter)
//创建routes数组
const routes=[
    {
        path:'/login',
        component:()=>import('@/views/Login')
    },
    {
        path:'/register',
        component:()=>import('@/views/Register')
    },
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/home',
        component:()=>import('@/views/Home'),
        children:[
            {
               path:'workplace',//二级路由的path的前不要带/
               component:()=>import('@/views/dashboard/workplace.vue'),
               meta:{
                firstTitle:'控制面板',
                secondTitle:'工作台',
                iskeepAlive:true
               }
            },
            {
                path:'analysis',
                component:()=>import('@/views/dashboard/analysis.vue'),
                meta:{
                    firstTitle:'控制面板',
                    secondTitle:'统计报表',
                    iskeepAlive:false
                }
            },
            {
                path:'studentList',
                component:()=>import('@/views/students/studentList.vue'),
                meta:{
                    firstTitle:'学生管理',
                    secondTitle:'学生列表'
                }
            },
            {
                name:'xsxq',
                path:'studentDetail',
                component:()=>import('@/views/students/studentDetail.vue'),
                meta:{
                    firstTitle:'学生管理',
                    secondTitle:'学生详情'
                },
                children:[
                    {
                        path:'basic',
                        component:()=>import('@/views/students/studentDetailBasic.vue'),
                        meta:{
                            firstTitle:'学生管理',
                            secondTitle:'学生列表>基本介绍'
                        }
                    },
                    {
                        path:'project',
                        component:()=>import('@/views/students/studentDetailProject.vue'),
                        meta:{
                            firstTitle:'学生管理',
                            secondTitle:'学生列表>项目信息'
                        }
                    }
                ]
            },
            {
                name:'xsxg',
                path:'studentUpdate/:id',
                component:()=>import('@/views/students/studentUpdate.vue'),
                meta:{
                    firstTitle:'学生管理',
                    secondTitle:'修改学生'
                },
                props:true
            }
        ],
        //如果进入该路由或者该路由下的子路由都会进入到这个守卫函数
        beforeEnter:async function(to,from,next){
           let token=getToken()
           if(token){
             let {code}=await Vue.prototype.$api.users.getUserInfo()
             if(code){
                next()
             }
           }else{
             alert('您还没有登录，请登录')
             next('/login')
           }
        }
    }
]

const router=new VueRouter({
    routes
})
/*
router.beforeEach(async(to,from,next)=>{
    console.log('********进入路由全局前置守卫************');
    if(to.path=="/login"||to.path=="/register"){
        console.log('即将要进入的是登录或者注册组件');
        //放行进入
        next()
    }else{
        //获取token
        let token=getToken()
        if(token){
            let result=await api.users.getUserInfo()
            if(result.code){
                next()//放行进入
            }
        }else{
            alert('您还没有登录，请先登录')
            next('/login')
        }
    }
})
*/

//将这个路由器对象导出
export default router